package com.example.springpethospital.aspect;

import com.example.springpethospital.service.UserService;
import com.example.springpethospital.util.Utility;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class AdminAspect {
    @Autowired
    UserService userService;

    @Pointcut("@annotation(com.example.springpethospital.entity.AdminMethod)")
    private void pointCut() {
    }

    @Around(value = "pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        HttpServletRequest request = (HttpServletRequest) args.get(0);
        String token = request.getParameter("token");
        if (!userService.isAdmin(token)) {
            return Utility.newResponseObject("token验证失败，当前登录用户非管理员或登录信息过期");
        }
        return joinPoint.proceed();
    }
}